package com.pri.chat.nim;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallbackWrapper;
import com.netease.nimlib.sdk.msg.model.RecentContact;
import com.netease.nimlib.sdk.nos.NosService;
import com.netease.nimlib.sdk.nos.model.NosThumbParam;
import com.netease.nimlib.sdk.nos.util.NosThumbImageUtil;
import com.netease.nimlib.sdk.uinfo.model.UserInfo;
import com.pri.chat.R;

import java.util.List;

/**
 * 描述：添加类的描述
 *
 * @author wx5e48d604c2885118
 * @time 2020/4/10
 */
public class ChatListAdapter extends BaseQuickAdapter<RecentContact, BaseViewHolder> {

    public ChatListAdapter(int layoutResId, @Nullable List<RecentContact> data) {
        super(layoutResId, data);
    }
    /**
     * 如果图片是上传到云信服务器，并且用户开启了文件安全功能，那么这里可能是短链，需要先换成源链才能下载。
     * 如果没有使用云信存储或没开启文件安全，那么不用这样做
     */
    private void changeUrlBeforeLoad(String roomId, final String url, final int defaultResId,
                                     final int thumbSize,ImageView imageView) {
        if (TextUtils.isEmpty(url)) {
            // avoid useless call
            loadImage(url, defaultResId, thumbSize,imageView);
        } else {
            /*
             * 若使用网易云信云存储，这里可以设置下载图片的压缩尺寸，生成下载URL
             * 如果图片来源是非网易云信云存储，请不要使用NosThumbImageUtil
             */
            NIMClient.getService(NosService.class).getOriginUrlFromShortUrl(url).setCallback(
                    new RequestCallbackWrapper<String>() {

                        @Override
                        public void onResult(int code, String result, Throwable exception) {
//                            if (TextUtils.isEmpty(result)) {
//                                result = url;
//                            }
//                            final String thumbUrl = makeAvatarThumbNosUrl(result, thumbSize);
                            loadImage(result, defaultResId, thumbSize,imageView);
                        }
                    });
        }
    }
    /**
     * 生成头像缩略图NOS URL地址（用作ImageLoader缓存的key）
     */
    private static String makeAvatarThumbNosUrl(final String url, final int thumbSize) {
        if (TextUtils.isEmpty(url)) {
            return url;
        }
        return thumbSize > 0 ? NosThumbImageUtil.makeImageThumbUrl(url,
                NosThumbParam.ThumbType.Crop,
                thumbSize, thumbSize) : url;
    }
    /**
     * ImageLoader异步加载
     */
    private void loadImage(final String url, final int defaultResId, final int thumbSize, ImageView imageView) {
        RequestOptions requestOptions = new RequestOptions().centerCrop().placeholder(defaultResId)
                .error(defaultResId).override(thumbSize,
                        thumbSize);
        Glide.with(mContext.getApplicationContext()).asBitmap().load(url).apply(requestOptions)
                .into(imageView);
    }
    public static final int DEFAULT_AVATAR_THUMB_SIZE = (int) NimUIKit.getContext().getResources()
            .getDimension(
                    com.netease.nim.uikit.R.dimen.avatar_max_size);

    public static final int DEFAULT_AVATAR_NOTIFICATION_ICON_SIZE = (int) NimUIKit.getContext()
            .getResources()
            .getDimension(
                    com.netease.nim.uikit.R.dimen.avatar_notification_size);

    private static final int DEFAULT_AVATAR_RES_ID = com.netease.nim.uikit.R.drawable.nim_avatar_default;
    /**
     * 加载用户头像（默认大小的缩略图）
     *
     * @param account 用户账号
     */
    public void loadBuddyAvatar(String account,ImageView imageView) {
        final UserInfo userInfo = NimUIKit.getUserInfoProvider().getUserInfo(account);
        changeUrlBeforeLoad(null, userInfo != null ? userInfo.getAvatar() : null,
                DEFAULT_AVATAR_RES_ID, DEFAULT_AVATAR_THUMB_SIZE,imageView);
    }
    @Override
    protected void convert(BaseViewHolder helper, RecentContact item) {
        helper.setText(R.id.tv_comment_name,item.getFromNick());
        helper.setText(R.id.tv_comment_age,item.getContent());
        loadBuddyAvatar(item.getContactId(),(ImageView)helper.getView(R.id.iv_logo));
        if(item.getUnreadCount()==0){
            helper.getView(R.id.iv_red).setVisibility(View.INVISIBLE);
        }else {
            helper.getView(R.id.iv_red).setVisibility(View.VISIBLE);
        }
    }
}
